package com.bytic.miaosha.controller;

import com.bytic.miaosha.vo.LoginVO;
import com.bytic.miaosha.result.CodeMsg;
import com.bytic.miaosha.result.Result;
import com.bytic.miaosha.service.MiaoshaUserService;
import com.bytic.miaosha.utils.ValidatorUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Controller
@RequestMapping("/login")
public class LoginController {

    private static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private MiaoshaUserService miaoshaUserService;

    @RequestMapping("/tologin")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("/dologin")
    @ResponseBody
    public Result<String> doLogin(HttpServletResponse response, @Valid LoginVO loginVO) {
        logger.info(loginVO.toString());
        // 校验，已使用校验器
//        String mobile = loginVO.getMobile();
//        String password = loginVO.getPassword();
//        if (StringUtils.isEmpty(mobile)) {
//            return Result.error(CodeMsg.MOBILE_EMPTY);
//        }
//        if (!ValidatorUtils.isMobile(mobile)) {
//            return Result.error(CodeMsg.MOBILE_ERROR);
//        }
//        if (StringUtils.isEmpty(password)) {
//            return Result.error(CodeMsg.PASSWORD_EMPTY);
//        }
        // 登录
        String token = miaoshaUserService.login(response, loginVO);
        return Result.success(token);
    }
}
